package com.example.demo.controller;/**
 * @Auter 李孝在
 * @Date 2025/1/16
 */

import cn.hutool.core.io.FileUtil;
import com.example.demo.resp.ResultData;
import com.example.demo.tool.FileDownTool;
import com.example.demo.tool.FileUploadTool;
import com.example.demo.tool.fileEnums.FileSizeLimitEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @projectName: demo
 * @package: com.example.demo.config.controller
 * @className: FileController
 * @author: Eric
 * @description: TODO
 * @date: 2025/1/16 15:08
 * @version: 1.0
 */
@RestController
@RequestMapping("/file")
public class FileController {



    @Tag(name = "文件上传")
    @Operation(summary = "文件上传")
    @Parameter(name = "file", description = "文件", required = true, schema = @Schema(implementation = MultipartFile.class))
    @PostMapping("/fileUpload")
    public ResultData<String> fileUpload(@RequestParam("file") MultipartFile file) throws IOException {
        FileUploadTool.fileUpload(file, FileSizeLimitEnum.M10);
        return ResultData.success("文件上传成功");
    }

    @Tag(name = "文件上传")
    @Operation(summary = "文件批量上传")
    @Parameter(name = "file", description = "文件", required = true, schema = @Schema(implementation = MultipartFile.class))
    @PostMapping("/fileBatchUpload")
    public ResultData<String> fileUpload(@RequestParam MultipartFile[] file) throws IOException {
        for (var multipartFile : file) {
            FileUploadTool.fileUpload(multipartFile);
        }
        return ResultData.success("文件上传成功");
    }


    @Resource
    private HttpServletResponse response;

    @Resource
    private HttpServletRequest request;

    @Tag(name = "文件下载")
    @PostMapping("/fileDownload")
    @Operation(summary = "单文件下载")
    public ResultData<String> fileDownload(@RequestBody String filePath,String fileName) throws IOException {


        FileDownTool.downloadFile(new File(fileName),request,response,fileName);
        return ResultData.success("下载成功");
    }

    @Tag(name = "文件下载")
    @PostMapping("/fileDownloadOnlyFile")
    @Operation(summary = "单文件下载:只传路径")
    public ResultData<String> fileDownloadOnlyFile(@RequestParam String filePath) throws IOException {

        File file=new File(filePath);
        String fileName = FileUtil.getName(filePath);
        FileDownTool.downloadFile(file,request,response,fileName);
        return ResultData.success("下载成功");
    }

    @Tag(name = "文件下载")
    @PostMapping("/zipDownload")
    @Operation(summary = "压缩包下载")
    public ResultData<String> fileDownloadZip(@RequestParam String filePath) throws IOException {


        List<File> zipFiles = FileUtil.loopFiles(filePath);
        String rootFilePath=filePath;
        FileDownTool.zipDirFileToFile(rootFilePath,zipFiles,request,response);
        return ResultData.success("下载成功");
    }


}
